package com.wjc.myktapplication.cusview

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View

/**
 * ClassName: com.wjc.myktapplication.cusview
 * Description:
 * JcChen on 2021.04.03.11:14
 */
class CusView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val paint = Paint()
    private val path = Path()




    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

     // 自定View，不需实现，ViewGroup 才需要调用它来确定子View的布局
//
//    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
//        super.onLayout(changed, left, top, right, bottom)
//    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        canvas.drawRGB(100, 200, 100);
//        drawArcTest(canvas)
        drawPathTest01(canvas)
//        drawPathTest02(canvas)
//        drawLine(canvas)
//        canvas.drawColor(Color.BLUE)
    }

    //    xxxTo() ——画线（直线或曲线）
    fun drawLine(canvas: Canvas) {
        paint.style = Paint.Style.STROKE
        path.lineTo(50f, 50f)
        path.rLineTo(50f, 0f)
        path.close()
        path.rMoveTo(30f, 30f)
        path.arcTo(100f, 100f, 300f, 300f, -90f, 90f, true)  // true:  强制移动到弧形起点（无痕迹）
        canvas.drawPath(path, paint)
    }

    //    addXxx() ——添加子图形
    private fun drawPathTest02(canvas: Canvas) {
        path.addCircle(300f, 300f, 200f, Path.Direction.CW);
        canvas.drawPath(path, paint)
    }

    private fun drawPathTest01(canvas: Canvas) {
        paint.style = Paint.Style.STROKE
        path.addArc(200f, 200f, 400f, 400f, -225f, 225f)
        path.arcTo(400f, 200f, 600f, 400f, -180f, 225f, false)
        path.lineTo(400f, 542f)
        path.close()
    }

    private fun drawArcTest(canvas: Canvas) {
        paint.style = Paint.Style.FILL
        canvas.drawArc(200f, 100f, 800f, 500f, -100f, 60f, true, paint) // 绘制扇形
        canvas.drawArc(200f, 100f, 800f, 500f, 20f, 140f, false, paint) // 绘制弧形
        paint.style = Paint.Style.STROKE
        // 绘制不封口的弧形
        canvas.drawArc(200F, 100F, 800F, 500F, 180F, 60F, false, paint)
    }
}